/**
 * Created by sunyx on 2016/9/29.
 */
import React, { PropTypes } from 'react';
import FormModal from '../../components/FormModal';
import {post,get} from '../../logics/rpc';
import md5 from "../../logics/md5";
import alert from '../../utils/alert';
import {Select, Upload, message, Button, Icon,Form, Input } from 'antd';
const Option = Select.Option;

// for (let i = 1; i < 3; i++) {
//   children.push(<Option key={i.toString(2) + i}>{i.toString(2) + i}</Option>);
// }
@Form.create()
export default class ChangeProgtam extends React.Component {
    onReturn = ev => {
        this.refs.form.submit();
    };
    componentWillMount = async()=>{
        console.log('cccc====','aaaa')
        let uploadInfo = await post('/admin/upload');
        console.log('uplodinfo',uploadInfo)
        this.setState({
            uploadInfo:uploadInfo
        });
    }
    componentWillReceiveProps = nextProps => {
        console.log('nextProps', nextProps);
        this.setState({...nextProps});
    }
    submit = async ({}) => {
        let imag = '' ;
        for(var i=0;i<this.state.images.length;i++){

            imag += this.state.images[i].response.hash+',';
        }
        let image = imag.substring(0,imag.length-1);
        console.log('image=======',image);
         //let gradeArr = this.state.gradename.split(',');
        // await upload();
        await post('/admin/game/change', {
            id:this.state.id,name:this.state.name,mobile_icon:this.state.mobile_icon,description:this.state.description,
            iosurl:this.state.iosurl,androidurl:this.state.androidurl,bigimage:this.state.bigimage,image
        });
        alert('创建成功');
        const {onAdded} = this.props;
        onAdded && onAdded();
    };
    state = {
        files: [],
        uploadInfo:{},
        mobile_icon:'',
        description:'',
        androidurl:'',
        iosurl:'',
        bigimage:'',
        images:[]
    };
    onChange = ({fileList}) => {
        fileList.forEach(({status,name, uid, response}) => {
            if (status === 'done') {
                message.success(`${name} file uploaded successfully`);
                this.setState({
                    mobile_icon:response.hash
                });
            }
        })
    };
    onZhuti = ({fileList}) => {

        fileList.forEach(({status,name, uid, response}) => {

            if (status === 'done') {
                message.success(`${name} file uploaded successfully`);
                this.setState({
                    bigimage:response.hash
                });
            }
        })
    };
    onXuanchuan = ({fileList}) => {
        this.setState({
            images:fileList
        })
        console.log('file=========',fileList);
    };
    getName= (value) =>{
        console.log('value',value, value.target, value.target.value);
        this.setState({
            name:value.target.value
        });
    }
    getDescription= (value) =>{
        this.setState({
            description:value.target.value
        });
    }
    getIosurl= (value) =>{

        this.setState({
            iosurl:value.target.value
        });
    }
    getAndroidurl= (value) =>{

        this.setState({
            androidurl:value.target.value
        });
    }
    render=()=> {
        console.log('111111111',this.state);
        const {uploadInfo} = this.state;
        const { fields } = this;
        const { getFieldProps } = this.props.form;
        if(uploadInfo) {
            return (
                <FormModal
                    ref="form"
                    title="添加资质"
                    okTitle="确 定"
                    onSubmit={this.submit}
                    {...this.props}
                >
                    <Form.Item label="游戏名">
                        <Input
                            onPressEnter={this.onReturn}
                            value={this.state.name}
                            onChange={this.getName}/>
                    </Form.Item>
                    <Form.Item label="游戏图标">
                        <Upload
                            action={uploadInfo.url}
                            name={uploadInfo.fieldName}
                            data={uploadInfo.formData}
                            showUploadList={true}
                            onChange={this.onChange}
                            multiple
                        >
                            <Button type="ghost">
                                <Icon type="upload" />
                            </Button>
                        </Upload>
                    </Form.Item>
                    <Form.Item label="游戏主题图">
                        <Upload
                            action={uploadInfo.url}
                            name={uploadInfo.fieldName}
                            data={uploadInfo.formData}
                            showUploadList={true}
                            onChange={this.onZhuti}
                            multiple
                        >
                            <Button type="ghost">
                                <Icon type="upload" />
                            </Button>
                        </Upload>
                    </Form.Item>
                    <Form.Item label="游戏宣传图">
                        <Upload
                            action={uploadInfo.url}
                            name={uploadInfo.fieldName}
                            data={uploadInfo.formData}
                            showUploadList={true}
                            onChange={this.onXuanchuan}
                        >
                            <Button type="ghost">
                                <Icon type="upload" />
                            </Button>
                        </Upload>
                    </Form.Item>
                    <Form.Item label="游戏描述">
                        <Input
                            onPressEnter={this.onReturn}
                            value={this.state.description}
                            onChange={this.getDescription}/>
                    </Form.Item>
                    <Form.Item label="游戏下载ios地址">
                        <Input
                            onPressEnter={this.onReturn}
                            value={this.state.iosurl}
                            onChange={this.getIosurl}/>
                    </Form.Item>
                    <Form.Item label="游戏下载android地址">
                        <Input
                            onPressEnter={this.onReturn}
                            value={this.state.androidurl}
                            onChange={this.getAndroidurl}/>
                    </Form.Item>
                </FormModal>
            );
        }
    }
}
